package in.muton.edushare.components.tasks.electives;

import in.muton.edushare.components.tasks.electives.api.ElectivePanelInitInfo;
import in.muton.edushare.components.tasks.electives.api.UserElectiveManager;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.processing.objects.UserDataHandler;

import java.util.List;

import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;

import com.edushare.common.vo.Universities;
import com.edushare.common.vo.Users;

public class ElectiveMainPanel extends EdusharePanel {

	private static final long serialVersionUID = -8759939478807639768L;

	public ElectiveMainPanel(String id, final IModel<Users> userModel, final EduShareBasePage page) {
		super(id, page);
		this.userModel = userModel;
		add(CSSPackageResource.getHeaderContribution(ElectiveMainPanel.class,
				"Elective.css"));
		add(new ListView<ElectivePanelInitInfo>("electiveCommonPanelsLV",
				new IModel<List<ElectivePanelInitInfo>>() {
					private static final long serialVersionUID = -8409308586457453271L;
					public List<ElectivePanelInitInfo> getObject() {
						return new UserElectiveManager(userModel)
								.getCommonPanels();
					}
					public void setObject(List<ElectivePanelInitInfo> arg0) {
					}
					public void detach() {
					}
				}) {
			@Override
			protected void populateItem(ListItem<ElectivePanelInitInfo> arg0) {
				ElectivePanelInitInfo epii = arg0.getModelObject();
				if (epii.getStatus() == ElectivePanelInitInfo.PENDING)
					arg0.add(new CommonElectivePanel("panel", page, epii.getCurr(),
							userModel, epii.getMultipleGroupIds(), epii
									.getCurrSemester()));
				else
					arg0.add(new CompletedCommonElectivePanel("panel", epii
							.getCurr(), userModel, epii.getMultipleGroupIds(),
							epii.getCurrSemester()));
			}

			private static final long serialVersionUID = 1L;
		});
		add(new ListView<ElectivePanelInitInfo>("electiveUniquePanelsLV",
				new IModel<List<ElectivePanelInitInfo>>() {
					private static final long serialVersionUID = -2298324597277665283L;

					public List<ElectivePanelInitInfo> getObject() {
						return new UserElectiveManager(userModel)
								.getUniquePanels();
					}

					public void setObject(List<ElectivePanelInitInfo> arg0) {
					}

					public void detach() {
					}
				}) {
			@Override
			protected void populateItem(ListItem<ElectivePanelInitInfo> arg0) {
				ElectivePanelInitInfo epii = arg0.getModelObject();
				if (epii.getStatus() == ElectivePanelInitInfo.PENDING)
					arg0.add(new UniqueElectivePanel("panel", page, epii.getCurr(),
							userModel, epii.getCurrSemester(), epii
									.getSingleGroupId()));
				else
					arg0.add(new CompletedUniqueElectivePanel("panel", epii
							.getCurr(), userModel, epii.getCurrSemester(), epii
							.getSingleGroupId()));

			}

			private static final long serialVersionUID = -6892992655243462845L;
		});
	}

	@Override
	public boolean isVisible() {
		boolean univOpen = false;
		UserDataHandler udh = new UserDataHandler(userModel);
		List<IModel<Universities>> univList = udh.getUniv().getObject();
		for (IModel<Universities> un : univList) {
			if (un.getObject().getElectiveSelectionTime() == Universities.UNIV_ALLOW_ELECTIVE_SELECTION)
				univOpen = true;
		}

		if (univOpen == false)
			return false;
		if (getLoggedInUser() != userModel.getObject())
			return false;
		if (new UserElectiveManager(userModel).getCommonPanels().size() > 0)
			return true;
		if (new UserElectiveManager(userModel).getUniquePanels().size() > 0)
			return true;
		return false;
	}

	private IModel<Users> userModel = null;
}
